package com.woniuxy.ticketorder.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.ticket.commons.entity.ResponseResult;
import com.woniuxy.ticketorder.order.entity.Order;
import com.woniuxy.ticketorder.order.entity.dto.OrderQO;
import com.woniuxy.ticketorder.order.service.OrderService;
import com.woniuxy.ticketorder.order.service.Schedule_seatService;
import com.woniuxy.ticketorder.order.service.UserService;
import com.woniuxy.ticketorder.order.util.OrderNumberCreator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;


@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private UserService userService;
    @Resource
    private Schedule_seatService schedule_seatService;
    @Resource
    private OrderService orderService;

    @ApiOperation("添加订单，锁座位，给用户加积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleSeatId", value = "排片-座位中间表主键", defaultValue = "1", required = true)
    }
    )
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Order order) {
        //通过登录账号获取userId
        int userId = 4;

        //调用cinema服务中方法，获取数据，并锁定位置，如果锁定失败返回null
        String str = schedule_seatService.findToOrderById(order.getScheduleSeatId());
        if (str != null && str.trim() != "") {
            //订单基本属性赋值
            order.setOrderno(OrderNumberCreator.createOrderNum());
            order.setOrdertime(new Date());
            order.setUserId(userId);
            order.setStatus("0");

            String[] strs = str.split(",");

            //封装订单数据
            order.setMoney(new BigDecimal(strs[0]));
            order.setCinema(strs[1]);
            order.setHall(strs[2]);
            order.setSeat(strs[3]);

            //将Order添加进数据库
            orderService.save(order);

            //给用户添加积分
            userService.updateIntegration(userId);

            return new ResponseResult(200,"添加订单成功！");
        }
        return new ResponseResult(500,"添加订单失败！");
    }

    @ApiOperation("订单多条件分页查询")
    @GetMapping("/list")
    public ResponseResult<Page<Order>> list(@RequestBody OrderQO orderQO) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (orderQO.getOrderno() != null && orderQO.getOrderno().trim().length() != 0) {
            queryWrapper.eq("orderno", orderQO.getOrderno());
        }
        if (orderQO.getMaxTime() != null) {
            queryWrapper.le("ordertime", orderQO.getMaxTime());
        }
        if (orderQO.getMinTime() != null) {
            queryWrapper.ge("ordertime", orderQO.getMinTime());
        }
        Page<Order> page = new Page<>(orderQO.getP() == null ? 1 : orderQO.getP(), 2);
        page = orderService.page(page, queryWrapper);
        return new ResponseResult<Page<Order>>().success(page);
    }

}
